package com.easy.controller;

import com.easy.bean.User;
import com.easy.service.IUserService;
import com.easy.util.LayData;
import com.easy.util.Page;
import com.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getUserList(Page page, String text){
        List<User> list=userService.selectUser(page,text);
        int count=userService.UserCount(text);
        return new LayData(count,list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public int deleteUser(int id){
        return userService.deleteUser(id);
    }

    @RequestMapping("add")
    @ResponseBody
    public int addUser(User user){
        user.setPassword(SYS.digest(user.getPassword()));
        return userService.addUser(user);
    }

    @RequestMapping("register")
    @ResponseBody
    public String register(User user,String confirmpwd){
        boolean check1=SYS.check("^[a-zA-Z0-9_]{6,20}$",user.getUsername(),user.getPassword(),confirmpwd);
        if(!check1){
            return "格式不正确！";
        }
        boolean check2=SYS.check("^1[3-9]\\d{9}$",user.getPhone());
        if(!check2){
            return "格式不正确！";
        }
        if(!verifyUsername(new Page(),null,user.getUsername())) {
            return "当前用户名重复！";
        }
        if(!user.getPassword().equals(confirmpwd)){
            return "密码和确认密码不一致！";
        }
        user.setPassword(SYS.digest(user.getPassword()));
        userService.addUser(user);
        return "注册成功！";
    }

    @RequestMapping("edit")
    @ResponseBody
    public int editUser(HttpSession session,User user){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if(user.getId()==0){
            user.setId(loginUser.getId());
        }
        if(user.getPassword()!=null){
            user.setPassword(SYS.digest(user.getPassword()));
        }
        return userService.editUser(user);
    }

    @RequestMapping("get")
    @ResponseBody
    public User getUserById(HttpSession session){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int id=loginUser.getId();
        return userService.getUserById(id);
    }

    @RequestMapping("verifyPassword")
    @ResponseBody
    public boolean verifyPassword(HttpSession session,User user){
        User loginUser=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        user.setId(loginUser.getId());
        return SYS.checkPass(user.getPassword(),userService.getUserById(user.getId()).getPassword());
                //user.getPassword().equals(userService.getUserById(user.getId()).getPassword());
    }

    @RequestMapping("verifyUsername")
    @ResponseBody
    public boolean verifyUsername(Page page,String text,String username){
        List<User> list=userService.selectUser(page,text);
        for(User item:list){
            if(item.getUsername().equals(username)){
                return false;
            }
        }
        return true;
    }

    @RequestMapping("logout")
    @ResponseBody
    public void logout(HttpSession session){
        if (session != null) {
            session.removeAttribute(SYS.SHOPPING_LOGIN_USER);
        }
    }

}
